// pages/return/return.js
var app=getApp();
import regeneratorRuntime from '../../utils/regenerator-runtime/runtime.js'
import {
  wxUpload
} from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resonarr: ['不喜欢','不想买了','商品质量问题','商品错发漏发','拍错了','其他原因'],
    index:0,//原因
    orderInfo:null,
    imgPath:app.globalData.imgPath,
    pzimgs:[],
    haschage:false,
    returnType:1,//1是申请退款2是申请退货
    tempFilePaths:[],//图片临时地址
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let type=options.type;
      let self=this;
      if(type==2){
        this.setData({returnType:type})
        wx.setNavigationBarTitle({
          title: '申请退款',
        })
      }else if(type==1){
        this.setData({returnType:type})
        wx.setNavigationBarTitle({
          title: '申请退货',
        })
      }
      let id=options.id;
      wx.request({
        url:app.globalData.reqPath+'shop/orders/'+id,
        header:{
          'authorization': app.globalData.authorization
        },
        success:function({data}){
          self.setData({ orderInfo:data.data})
        }
      })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeReson:function(e){
    this.setData({haschage:true,index:e.detail.value});
  },
  upImg:function(){
    let self = this;
    wx.chooseImage({
      count:9-this.data.pzimgs.length,
      success: function (res) {
        let tempFilePaths = res.tempFilePaths;
         console.log(tempFilePaths)
        self.setData({ tempFilePaths: tempFilePaths })
        self.uploadImg(tempFilePaths,0)
      },
    })
  },
  del: function (e) {
    let index = e.currentTarget.dataset.index;
    let list = this.data.pzimgs;
    list.splice(index, 1);
    this.setData({ pzimgs: list });
  },
  sub:function(e){
    if (this.data.haschage==false){
      wx.showToast({
        title: '请选择原因',
        icon:'none'
      })
      return false;
    }
    if (this.data.pzimgs.length==0){
      wx.showToast({
        title: '请上传凭证',
        icon:'none'
      })
      return false;
    }
    let data = {
      orderId: this.data.orderInfo.id,
      reason: this.data.resonarr[this.data.index],
      imgs: this.data.pzimgs.join(),
      desc: this.data.desc,
      refundway: this.data.returnType
    }
    wx.request({
      url: app.globalData.reqPath + 'shop/OrdersStatusRefund',
      method: 'POST',
      data: data,
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data }) {
        if (data.code == 200) {
          wx.showToast({
            title: '提交成功',
            icon:'none'
          })
          setTimeout(function(){
            wx.redirectTo({
              url: '/pages/order/order?s=2',
            })
          },500)
          
        } else {
          wx.showToast({
            title: data.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
   uploadImg: async function (imgs, i) {
    let files = imgs;
    i ? i : 0
    let len = files.length;
    let self = this;
    let upimgs = this.data.pzimgs;
    if (i < len) {
      await wxUpload(app.globalData.reqPath + 'shop/upload', { file: files[i], 'authorization': app.globalData.authorization }).then(function (res) {
        console.log(res)
        let result = JSON.parse(res)
        if (result.code == 200) {
          i++;
          console.log(i)
          console.log(result.data) 
          console.log("2222" + self.data.pzimgs)  
          upimgs.push(result.data);
          self.setData({pzimgs: upimgs})
          console.log("3333"+self.data.pzimgs)        
          self.uploadImg(self.data.tempFilePaths,i);
        }
      })
    }

  },
})